///<reference path="../../node_modules/egg/index.d.ts"/>
import { Service } from 'egg';

export default class userService extends Service {

    public async getList() {
        const {ctx} = this;
        const userList = await ctx.model.Users.find({})
        return userList
    }

    async save(data) {
        /*todo save和基于save的ctx.model.Users.create方法,返回的是promise对象
        * 建议使用create方法*/
        const {ctx} = this;
        let result;
        result = ctx.helper.createData(data,ctx.model.Users).then(res=>{
            return res
        }).catch(err=>{
            console.log(err);
        });

        return result;
    }

    async update(userData:object) {
        const {ctx} = this;
        let query={_id: userData['_id']}
        let result;
        result = ctx.helper.updateById(query, userData,ctx.model.Users).then(res=>{
            return res
        }).catch(err=>{
            console.log(err);
        });
        return result;

    }
    public async delete(id:string) {
        const {ctx} = this;
        let whereStr = {'_id': id}
        let result:object={};

        await ctx.model.Users.remove(whereStr).then((res:object)=>{
            result = res;
        }).catch((err:any)=>{
            console.log(err)
        })
        return result
    }
}